<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite ptb20 radius10 pl10 pr20 justify-between">
      <div>
        <div class="align-center  ">
          <el-date-picker v-model="start" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>


      </div>
      <div>
        <el-button class="btn2" @click="getPromotionList()">查詢</el-button>
        <el-button class="btn1" @click="reset(1)">重置</el-button>
      </div>
    </div>
    <div class="bgblueg ptb30 radius10 w100p mt15 plr80 justify-between">
      <div class="align-center clr3c4 flex1">
        <div>
          <div class="ft46">{{ promotionTotal }}</div>
          <div>推廣佣金總額（元）</div>
        </div>
      </div>

    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="tableData" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">

          <el-table-column prop="user" label="推廣用戶訊息" width="200">
            <template #default="scope">
              <span @click="showUser(scope.row.user.id)">{{ scope.row.user.phone }}(ID:{{ scope.row.user.id }})</span>

            </template>
          </el-table-column>
          <el-table-column prop="user.name" label="推廣用戶昵稱" />
          <el-table-column prop="count" label="推廣人數">
            <template #default="scope">
              <div class="align-center" @click="userDialogShow(scope.row)">
                <span class="mr5">{{ scope.row.count }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>

            </template>
          </el-table-column>
          <el-table-column prop="money" label="總收入">
            <template #default="scope">
              <div class="align-center" @click="incomeDialogShow(scope.row)">
                <span class="mr5">{{ scope.row.money }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>

            </template>
          </el-table-column>
          <!-- <el-table-column prop="address" label="創建時間" width="260" /> -->
        </el-table>
      </div>
      <div class="plr20 mlauto">
        <div>小計(元): <span class="ft46 clr3c4 ml10">{{ promotionTotal }}</span> </div>
      </div>
      <div class="plr20 mlauto mt20">
        <el-pagination v-model:current-page="currentPage1" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total1" @current-change="handleCurrentChange1" />
      </div>

    </div>
    <el-dialog v-model="dialogTableVisible" title="推廣人數詳情">
      <div class="plr20 justify-between ptb20">
        <div class="align-center  ">
          <el-date-picker v-model="start2" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end2" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>
        <div>
          <el-button class="btn2" @click="getPromotionUserDetail()">查詢</el-button>
          <el-button class="btn1" @click="reset(2)">重置</el-button>
        </div>

      </div>
      <el-table :data="userTableData" max-height="450" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
        <el-table-column property="id" label="用戶訊息" min-width="180" />
        <el-table-column property="name" label="用戶昵稱" min-width="180" />
        <el-table-column property="avatar" label="用戶頭像" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="50" :fit="'cover'" :src="scope.row.avatar" />
          </template>

        </el-table-column>
        <el-table-column property="login_type_text" label="注冊方式" width="150" />
        <el-table-column property="created_at" label="注冊時間" min-width="180" />

      </el-table>
      <div class=" plr20 justify-between mt20">
        <div class="clr3c4 bold noshrink">總共{{ total2 }}條</div>
        <div>
          <el-pagination v-model:current-page="currentPage2" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total2" @current-change="handleCurrentChange2" />
        </div>
      </div>
    </el-dialog>
    <el-dialog v-model="incomeTableVisible" title="收入詳情">
      <div class="plr20 justify-between ptb20">
        <div class="align-center  ">
          <el-date-picker v-model="start3" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end3" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>
        <div>
          <el-button class="btn2" @click="getPromotionIncomeDetail()">查詢</el-button>
          <el-button class="btn1" @click="reset(3)">重置</el-button>
        </div>

      </div>
      <el-table :data="incomeTableData" max-height="500" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
        <el-table-column property="user.id" label="推廣用戶訊息" min-width="200" />
        <el-table-column property="user.name" label="推廣用戶昵稱" min-width="200" />
        <el-table-column property="money" label="佣金">
        </el-table-column>
        <el-table-column property="created_at" label="創建時間" min-width="200" />

      </el-table>
      <div class=" plr20 justify-between mt20">
        <div class="clr3c4 bold noshrink">總共{{ total3 }}條</div>
        <div>
          <div>小計(元): <span class="ft46 clr3c4 ml10">{{ PromotionIncomeTotal }}</span> </div>
        </div>
      </div>
      <div class="flex-col w100p">
        <div class="plr20 mt20 mlauto">
          <el-pagination v-model:current-page="currentPage1" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total3" @current-change="handleCurrentChange3" />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { promotionList, promotionUserDetail, promotionIncomeDetail } from '@/api/statement.js'
import { inject } from 'vue';
const showUser = inject('showUser');
onMounted(() => {
  getPromotionList()
})
const start = ref('')
const end = ref('')
const promotionTotal = ref(0)
// 推广报表
const getPromotionList = () => {
  promotionList({
    start: start.value,
    end: end.value
  }).then((res) => {
    console.log('推广报表', res)
    tableData.value = res.list.data
    total1.value = res.list.total
    promotionTotal.value = res.total
  })

}
const reset = (type) => {
  if (type == 1) {
    start.value = ''
    end.value = ''
    getPromotionList()
  }
  if (type == 2) {
    start2.value = ''
    end2.value = ''
    getPromotionUserDetail()
  }
  if (type == 3) {
    start3.value = ''
    end3.value = ''
    getPromotionIncomeDetail()
  }

}
const start2 = ref('')
const end2 = ref('')
//推广人数详情
const getPromotionUserDetail = () => {
  promotionUserDetail({
    uid: currentUid.value,
    start: start2.value,
    end: end2.value
  }).then((res) => {
    console.log('推广人数详情', res)
    userTableData.value = res.list.data
    total2.value = res.list.total
  })
}
const start3 = ref('')
const end3 = ref('')
const PromotionIncomeTotal = ref(0)
// 總收入詳情
const getPromotionIncomeDetail = () => {
  promotionIncomeDetail({
    uid: currentUid.value,
    start: start3.value,
    end: end3.value
  }).then((res) => {
    console.log('推广人数详情', res)
    incomeTableData.value = res.list.data
    total3.value = res.list.total
    PromotionIncomeTotal.value = res.total
  })
}
const currentUid = ref('')
const dialogTableVisible = ref(false)
const incomeTableVisible = ref(false)

// 推廣人數彈窗
const userDialogShow = (row) => {
  currentUid.value = row.uid
  getPromotionUserDetail()
  dialogTableVisible.value = true

}
// 總收入彈窗
const incomeDialogShow = (row) => {
  currentUid.value = row.uid
  getPromotionIncomeDetail()
  incomeTableVisible.value = true
}

const userTableData = ref([])
const incomeTableData = ref([])
const tableData = ref([])
const currentPage1 = ref(1)
const total1 = ref(0)
const handleCurrentChange1 = (val) => {
  console.log(`current page: ${val}`)
  getPromotionList()
}
const currentPage2 = ref(1)
const total2 = ref(0)
const handleCurrentChange2 = (val) => {
  console.log(`current page: ${val}`)
  getPromotionUserDetail()
}
const currentPage3 = ref(1)
const total3 = ref(0)
const handleCurrentChange3 = (val) => {
  console.log(`current page: ${val}`)
  getPromotionList()
}
const router = useRouter()
const toAdd = () => {
  console.log(router)
  router.push({ path: 'addNotice' })
}
const edit = (type) => {
  router.push({ path: 'signEdit' })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;s
}
</style>
<style></style>
 